######################################################################
# Time-stamp: <04/07/21 14:53:42 ostolop>
#
# Data Upload dynamic data routines
######################################################################
package EP::EPC::Interface::Published_Datasets;

use strict;

use EP::EPC::Interface;
use EP::Legacy::EPDATA;

use XML::LibXSLT;
use EP::Config;

my $DATA_UPLOAD_EPC_ID; # To be retrieved from the database!!

sub newDD {
  my $proto = shift;
  my $ep_query_doc = shift;
  my $query = shift;
  my $class = ref($proto) || $proto;
  my $self  = {};
  bless $self, $class;

  $self->initialize($ep_query_doc, $query);
  return $self;
}

sub initialize {
  my $self = shift;
  $self->{epc_query} = shift;
  $self->{query} = shift;

  $DATA_UPLOAD_EPC_ID = 1; # TODO: from database!

  return;
}

sub list_file_delim_types {
    my $self = shift;

    my $file_delim = &EP::Common::DB::get_dynamic_data("file_delim", 0);
    my $dl = &EP::EPC::Interface::DD::dynamic_list( $file_delim );

    return $dl->findnodes('/*');
}

sub list_species {
    my $self = shift;

    my $organism = &EP::Common::DB::get_dynamic_data("species", 0);
    my $dl = &EP::EPC::Interface::DD::dynamic_list( $organism );
    
    return $dl->findnodes('/*');
}

sub list_dataset_types {
    my $self = shift;

    my $organism = &EP::Common::DB::get_dynamic_data("dataset_type", 0);
    my $dl = &EP::EPC::Interface::DD::dynamic_list( $organism );

    return $dl->findnodes('/*');
}

sub list_published_datasets {
  my $self = shift;
  my $ORGANISM = shift;
  my $datasets;
  my ($count) = 0;

  EP::Legacy::EPDATA::define_available_data_sets();

  foreach my $DS ( @{$EP::Legacy::EPDATA::DATASETS{$ORGANISM}} ) {
      $datasets->{$count}{key} = "exp/$ORGANISM/$DS";
      $datasets->{$count}{value} = $DS;
      $datasets->{$count}{name} = "DATASET";
      $count++;
  }

  my $dl = &EP::EPC::Interface::DD::dynamic_list( $datasets );
  return $dl->findnodes('/*');
}

sub list_published_organisms {
  my $self = shift;
  my $list_organism = shift; # optional argument to show just one organism...

  my $organisms;
  my ($count) = 0;

  EP::Legacy::EPDATA::define_available_data_sets();

  foreach ( @EP::Legacy::EPDATA::ORGANISMS ) {
      $organisms->{$count}{key} = $_;
      $organisms->{$count}{value} = $EP::Legacy::EPDATA::ORGANISM{$_};
      $count++;
  }

  my $dl = &EP::EPC::Interface::DD::dynamic_list ( $organisms );
  return $dl->findnodes('/*');
}

1;
